<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('用户列表')"/>
    <th:block th:include="include :: layout-latest-css"/>
</head>
<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="form-content">
            <form id="user-form" class="form-horizontal">
                <!--<input type="hidden" id="deptId" name="deptId">-->
                <input type="hidden" id="parentId" value="100" name="parentId">
                <div class="row">
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">归属部门：</label>
                            <div class="col-sm-8">
                                <select name="deptId" class="form-control">
                                    <option value="">所有</option>
                                    <option th:each="dept:${depts}" th:value="${dept.deptId}"
                                            th:text="${dept.deptName}"></option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">登录名称：</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input type="text" name="loginName" class="form-control"/>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">商户编号：</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input type="text" name="params[userId]" class="form-control"/>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">用户状态：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="status"
                                        th:with="type=${@dict.getType('sys_normal_disable')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                            th:value="${dict.dictValue}"></option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label ">创建开始时间：</label>
                            <div class="col-sm-4">
                                <div class="input-group select-time">
                                    <input type="text" autocomplete="off" class="time-input form-control"
                                           placeholder="开始时间"
                                           name="params[beginTime]"/>
                                </div>
                            </div>
                            <label class="col-sm-2 control-label">创建结束时间</label>
                            <div class="col-sm-4">
                                <div class="input-group select-time">
                                    <input type="text" class="time-input form-control" autocomplete="off"
                                           placeholder="结束时间"
                                           name="params[endTime]"/>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">用户名称：</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input type="text" name="userName" class="form-control" style="width: 316px;"/>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-4 col-md-offset-1">
                        <div class="form-group">
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="addBusiness()" shiro:hasPermission="system:user:addBusiness">
                <i class="fa fa-plus"></i> 新增会员
            </a>
            <a class="btn btn-success" onclick="addSuper()" shiro:hasPermission="system:user:addProxy">
                <i class="fa fa-plus"></i> 新增管理员
            </a>
            <a class="btn btn-success" onclick="addSalesman()" shiro:hasPermission="system:user:addProxy">
                <i class="fa fa-plus"></i> 新增业务员
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="false"></table>
        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: layout-latest-js"/>
<!--<th:block th:include="include :: ztree-js"/>-->
<script th:src="@{/js/md5.min.js}"></script>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:user:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:user:remove')}]];
    var resetPwdFlag = [[${@permission.hasPermi('system:user:resetPwd')}]];
    var datas = [[${@dict.getType('sys_normal_disable')}]];
    var prefix = ctx + "system/user";

    $(function () {
        var panehHidden = false;
        if ($(this).width() < 769) {
            panehHidden = true;
        }
        $('body').layout({initClosed: panehHidden, west__size: 185});
        queryUserList();
        queryDeptTree();
    });

    function queryUserList() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            importUrl: prefix + "/importData",
            importTemplateUrl: prefix + "/importTemplate",
            sortName: "createTime",
            sortOrder: "desc",
            modalName: "用户",
            columns: [{
                checkbox: true
            },
                {
                    field: 'userId',
                    title: '商户编号'
                },
                {
                    field: 'loginName',
                    title: '登录名称',
                    sortable: true
                },
                {
                    field: 'userName',
                    title: '用户名称'
                },
                {
                    field: 'dept.deptName',
                    title: '部门'
                },
                {
                    field: 'email',
                    title: '邮箱',
                    visible: false
                },
                {
                    field: 'phonenumber',
                    title: '手机',
                    visible: false
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
                    title: '用户状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return statusTools(row);
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.roleId == 101 || row.roleId == 106) {
                            // 商户
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="resetPayPwd(\'' + row.userId + '\')"><i class="fa fa-edit"></i>重置支付密码</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="editBusiness(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        } else if (row.roleId == 103) {
                            // 代理
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="addBusiness()"><i class="fa fa-edit"></i>新增子商户</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="querySubBusiness(\'' + row.userId + '\')"><i class="fa fa-edit"></i>查询子商户</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="editProxy(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        } else if (row.roleId == 104) {
                            // 支付商
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="addPayerRate(\'' + row.userId + '\')"><i class="fa fa-edit"></i>设置ip</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="editPayer(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        } else if (row.roleId == 107) {
                            // 业务员
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="editSalesman(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="salesmanRdate(\'' + row.userId + '\')"><i class="fa fa-edit"></i>设置汇率</a> ');
                        }  else if (row.roleId == 105) {
                            // 跑分码商
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="addScore(\'' + row.userId + '\')"><i class="fa fa-edit"></i>新增子代理</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="querySubScore(\'' + row.userId + '\')"><i class="fa fa-edit"></i>查询子代理</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="editScore(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        } else if (row.roleId == 100) {
                            // 通道商
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="rateSetting(\'' + row.userId + '\')"><i class="fa fa-edit"></i>费率设置</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="settingChannel(\'' + row.userId + '\')"><i class="fa fa-edit"></i>信息配置</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="editChannel(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="resetSendPwd(\'' + row.userId + '\')"><i class="fa fa-edit"></i>重置补单密码</a> ');
                        } else {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.editTab(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        }
                        actions.push('<a class="btn btn-success btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.userId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + resetPwdFlag + '" href="#" onclick="resetPwd(\'' + row.userId + '\')"><i class="fa fa-key"></i>重置</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    }

    function addProxy() {
        var url = ctx + "system/user/addProxy";
        $.modal.openTab("新增代理商", url);
    }

    function addPayer() {
        var url = ctx + "system/user/addPayer";
        $.modal.openTab("新增支付商", url);
    }
    function addSuper() {
        var url = ctx + "system/user/addSuper";
        $.modal.openTab("新增业务员", url);
    }
    function addSalesman() {
        var url = ctx + "system/user/addSalesman";
        $.modal.openTab("新增管理员", url);
    }
    function addChannel() {
        var url = ctx + "system/user/addChannel";
        $.modal.openTab("新增码商", url);
    }

    function addScore(id) {
        var url = ctx + "system/user/addScore/"+id;
        $.modal.openTab("新增跑分代理", url);
    }

    function editChannel(id) {
        var url = ctx + "system/user/editChannel/" + id;
        $.modal.openTab("修改码商", url);
    }

    function editSalesman(id) {
        var url = ctx + "system/user/editSalesman/" + id;
        $.modal.openTab("修改业务员", url);
    }

    function editScore(id) {
        var url = ctx + "system/user/editScore/" + id;
        $.modal.openTab("修改跑分代理", url);
    }

    function addBusinessRate(id) {
        var url = ctx + "channel/businessRate/" + id;
        $.modal.openTab("商户费率", url);
    }



    /**
     * 设置码商代理费率
     */
    function rateSetting(id) {
        var url = ctx + "channel/businessRate/channel/" + id;
        $.modal.openTab("码商代理费率", url);
    }

    function settingChannel(id) {
        var url = ctx + "channel/channelSetting/" + id;
        $.modal.openTab("码商代理配置", url);
    }


    // function addChannelRate(id) {
    //     var url = ctx + "channel/channelRate/" + id;
    //     $.modal.openTab("通道商费率", url);
    // }

    function addPayerRate(id) {
        var url = ctx + "channel/payerInfo/edit/" + id;
        $.modal.open("支付商IP", url);
    }

    function salesmanRdate(id) {
        // 业务员汇率
        $.ajax({
            type: "get",
            url: ctx + "channel/salesmanInfo/getUser/"+id,
            success: function(r) {
                if (r.code == 0) {
                    debugger
                    layer.open({
                        title: '请输入汇率'
                        ,content: '<div style="color: black;"><label>请输入汇率:&nbsp;&nbsp;</label><input id="salesmanRate" value="'+r.data.rate+'" type="text" placeholder="请输入业务员汇率" style="width: 200px"></div>\n',
                        btn: ['确定', '取消'],
                        btn1: function(index, layero){
                            var val = $('#salesmanRate').val();
                            if(val.trim()===''){
                                alert('请输入汇率');
                                return false;
                            }
                            var url = ctx + "channel/salesmanInfo/addOrUpdate";
                            var data = {"userId": id,"rate":val};
                            $.operate.submit(url, "post", "json", data);
                        },
                        btn2: function(index, layero){
                        }
                    });
                }
            }
        });
    }
    function capitalSetting(id) {
        var url = ctx + "channel/businessCaptialSetting/edit/" + id;
        $.modal.open("提现设置", url);
    }


    function editProxy(id) {
        var url = ctx + "system/user/editProxy/" + id;
        $.modal.openTab("编辑代理商", url);
    }

    function editPayer(id) {
        var url = ctx + "system/user/editPayer/" + id;
        $.modal.openTab("编辑支付商", url);
    }

    function resetPayPwd(id) {
        // $.ajax({
        //     type: "post",
        //     url: ctx + "sendPwdCode",
        //     data: {
        //
        //     },
        //     success: function(r) {
        //         if (r.code == 0) {
        //             layer.open({
        //                 title: '请输入email为'+r.data+'的验证码'
        //                 ,content: '<div style="color: black;"><label>请输入验证码:&nbsp;&nbsp;</label><input id="phoneCode" type="text" placeholder="请输入验证码" style="width: 200px"></div>\n',
        //                 btn: ['确定', '取消'],
        //                 btn1: function(index, layero){
        //                     var val = $('#phoneCode').val();
        //                     if(val.trim()===''){
        //                         alert('请输入验证码');
        //                         return false;
        //                     }
        //                     var url = ctx + "channel/businessInfo/resetPayPwd";
        //                     var data = {"id": id,"code":val};
        //                     $.operate.submit(url, "post", "json", data);
        //                 },
        //                 btn2: function(index, layero){
        //                 }
        //             });
        //         }
        //     }
        // });
        // $.modal.confirm("确认要重置密码吗?", function () {
        //     var url = ctx + "channel/businessInfo/resetPayPwd";
        //     var data = {"id": id,"code":''};
        //     $.operate.submit(url, "post", "json", data);
        // });
        layer.open({
            area: ['300px', '200px'],
            title: '请输入新密码',
            content: '<div><div style="color: black;"><label>请输入新密码:&nbsp;&nbsp;</label><input id="password" type="text" placeholder="请输入至少6位数的新密码" style="width: 400px"></div></div>',
            btn: ['确定', '取消'],
            btn1: function (index, layero) {
                var val = $('#password').val();

                if (val.trim() === '') {
                    alert('请输入密码');
                    return false;
                }
                if (val.trim().length <6) {
                    alert('请输入至少6位数的密码');
                    return false;
                }

                var url = ctx + "channel/businessInfo/resetPayPwd";
                var data = {"id": id,"code":'', "password": md5(val)};
                $.operate.submit(url, "post", "json", data);
            },
            btn2: function (index, layero) {
            }
        });
    }

    function resetSendPwd(id) {
        $.ajax({
            type: "post",
            url: ctx + "sendPwdCode",
            data: {

            },
            success: function(r) {
                if (r.code == 0) {
                    layer.open({
                        title: '请输入email为'+r.data+'的验证码'
                        ,content: '<div style="color: black;"><label>请输入验证码:&nbsp;&nbsp;</label><input id="phoneCode" type="text" placeholder="请输入验证码" style="width: 200px"></div>\n',
                        btn: ['确定', '取消'],
                        btn1: function(index, layero){
                            var val = $('#phoneCode').val();
                            if(val.trim()===''){
                                alert('请输入验证码');
                                return false;
                            }
                            var url = ctx + "channel/channelUserInfo/resetSendPwd";
                            var data = {"id": id,"code":val};
                            $.operate.submit(url, "post", "json", data);
                        },
                        btn2: function(index, layero){
                        }
                    });
                }
            }
        });
    }

    function editBusiness(id) {
        var url = ctx + "system/user/editBusiness/" + id;
        $.modal.openTab("修改代商户", url);
    }

    function addBusiness() {
        var url = ctx + "system/user/addBusiness";
        $.modal.openTab("新增商户", url);
    }

    function querySubBusiness(id) {
        var url = ctx + "system/user/businessList/" + id;
        $.modal.openTab("查询下级商户", url);
    }
    function querySubScore(id) {
        var url = ctx + "system/user/scoreList/" + id;
        $.modal.openTab("查询下级代理", url);
    }
    function queryDeptTree() {
        var url = ctx + "system/dept/treeData";
        var options = {
            url: url,
            expandLevel: 1,
            onClick: zOnClick
        };
        $.tree.init(options);

        function zOnClick(event, treeId, treeNode) {
            $("#deptId").val(treeNode.id);
            $("#parentId").val(treeNode.pId);
            $.table.search();
        }
    }

    $('#btnExpand').click(function () {
        $._tree.expandAll(true);
        $(this).hide();
        $('#btnCollapse').show();
    });

    $('#btnCollapse').click(function () {
        $._tree.expandAll(false);
        $(this).hide();
        $('#btnExpand').show();
    });

    $('#btnRefresh').click(function () {
        queryDeptTree();
    });

    /* 用户管理-部门 */
    function dept() {
        var url = ctx + "system/dept";
        createMenuItem(url, "部门管理");
    }

    /* 用户管理-重置密码 */
    function resetPwd(userId) {
        // $.ajax({
        //     type: "post",
        //     url: ctx + "sendPwdCode",
        //     data: {
        //
        //     },
        //     success: function(r) {
        //         if (r.code == 0) {
        //             layer.open({
        //                 title: '请输入手机号为'+r.data+'的验证码'
        //                 ,content: '<div style="color: black;"><label>请输入验证码:&nbsp;&nbsp;</label><input id="phoneCode" type="text" placeholder="请输入验证码" style="width: 200px"></div>\n',
        //                 btn: ['确定', '取消'],
        //                 btn1: function(index, layero){
        //                     var val = $('#phoneCode').val();
        //                     if(val.trim()===''){
        //                         alert('请输入验证码');
        //                         return false;
        //                     }
        //                     var url = prefix + '/resetPwd';
        //                     var data = {"userId": userId, "password": md5("123456"),"code":val};
        //                     $.operate.submit(url, "post", "json", data);
        //                 },
        //                 btn2: function(index, layero){
        //                 }
        //             });
        //         }
        //     }
        // });

        // $.modal.confirm("确认要重置密码吗?", function () {
        //     var url = prefix + '/resetPwd';
        //     var data = {"userId": userId, "password": md5("123456")};
        //     $.operate.submit(url, "post", "json", data);
        // });

        layer.open({
            area: ['300px', '200px'],
            title: '请输入新密码',
            content: '<div><div style="color: black;"><label>请输入新密码:&nbsp;&nbsp;</label><input id="password" type="text" placeholder="请输入至少6位数的新密码" style="width: 400px"></div></div>',
            btn: ['确定', '取消'],
            btn1: function (index, layero) {
                var val = $('#password').val();

                if (val.trim() === '') {
                    alert('请输入密码');
                    return false;
                }
                if (val.trim().length <6) {
                    alert('请输入至少6位数的密码');
                    return false;
                }

                var url = prefix + '/resetPwd';
                var data = {"userId": userId, "password": md5(val)};
                $.operate.submit(url, "post", "json", data);
            },
            btn2: function (index, layero) {
            }
        });
    }


    /* 用户状态显示 */
    function statusTools(row) {
        if (row.status == 1) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.userId + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.userId + '\')"></i> ';
        }
    }

    /* 用户管理-停用 */
    function disable(userId) {
        $.modal.confirm("确认要停用用户吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"userId": userId, "status": 1});
        })
    }

    /* 用户管理启用 */
    function enable(userId) {
        $.modal.confirm("确认要启用用户吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"userId": userId, "status": 0});
        })
    }
</script>
</body>
<form id="importForm" enctype="multipart/form-data" class="mt20 mb10" style="display: none;">
    <div class="col-xs-offset-1">
        <input type="file" id="file" name="file"/>

        <div class="mt10 pt5">
            <input type="checkbox" id="updateSupport" name="updateSupport" title="如果登录账户已经存在，更新这条数据。"> 是否更新已经存在的用户数据
            &nbsp; <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i
                class="fa fa-file-excel-o"></i> 下载模板</a>
        </div>
        <font color="red" class="pull-left mt10">
            提示：仅允许导入“xls”或“xlsx”格式文件！
        </font>
    </div>
</form>
</html>